package com.example.a828502.httpHelper;

/**
 * Created by 828502 on 2016/5/17.
 */

import com.example.a828502.Utils.GsonUtils;
import com.example.a828502.sfdialtestdemo.ListItem;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 将字节流转换为字符串的工具类
 */
public class HttpUtils {

    public static String readMyInputStream(InputStream is) {
        byte[] result;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024*1024];
            int len = 0;
            if((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            is.close();
            baos.close();
            result = baos.toByteArray();

        } catch (IOException e) {
            e.printStackTrace();
            String errorStr = "获取数据失败。";
            return errorStr;
        }
        return new String(result);
    }


    /**
     * JSON解析方法
     */
    public static Object JSONAnalysis(String string) {
        JSONObject object = null;
        try {
            object = new JSONObject(string);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        /**
         * 在你获取的string这个JSON对象中，提取你所需要的信息。
         */
//        if (object.optJSONObject("weatherinfo")!=null){
//            JSONObject ObjectInfo = object.optJSONObject("weatherinfo");
//            String city = ObjectInfo.optString("city");
//            String cityid = ObjectInfo.optString("cityid");
//            String windDir = ObjectInfo.optString("WD");
//            String time = ObjectInfo.optString("time");
//            String SD = ObjectInfo.optString("SD");
//            String weatherResult = "城市：" + city + "\n城市ID：" + cityid + "\n风向：" + windDir
//                    + "\n时间：" + time + "\n温度：" + SD ;
//
//            return weatherResult;
//        }

        return object;

    }


}